package priv.happy.mealpickpro.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import priv.happy.mealpickpro.domain.UserMeal;
import priv.happy.mealpickpro.dto.UserDTO;
import priv.happy.mealpickpro.emum.SystemStatusCode;
import priv.happy.mealpickpro.service.UserMealService;
import priv.happy.mealpickpro.vo.ApiResponse;

/**
 * @Author: HAPPY
 * @Project_name: mealPickPro
 * @Package_name: priv.happy.mealpickpro.controller
 * @Date: 10/15/2023 1:32 AM
 * @Description:
 */
@RestController
@RequestMapping("/api/v2/userinfo")
public class UserController {

    @Autowired
    UserMealService userMealService;

    /**
     * 根据id获取用户信息
     *
     * @param userId 用户id
     * @return 查询到的用户信息
     */
    @GetMapping("/{userId}")
    public ApiResponse<UserMeal> getUserInfo(@PathVariable Integer userId) {
        ApiResponse<UserMeal> apiResponse = new ApiResponse<>();
        if (userId == null) {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).setMessage("添加新标签失败").setSuccess(false);
        } else {
            UserMeal userMeal = userMealService.getById(userId);
            if (userMeal.getId() != null) {
                apiResponse.setCode(SystemStatusCode.SUCCESS.getCode()).setMessage("查询用户信息成功").setSuccess(true).setData(userMeal);
            }
        }
        return apiResponse;
    }

    /**
     * 根据id修改用户信息，不修改的值设置为null即可
     *
     * @param userInfo 要修改的用户信息
     * @return 返回修改后的用户新信息
     */
    @PutMapping()
    public ApiResponse<UserMeal> modifyUserInfo(@RequestBody UserDTO userInfo) {
        ApiResponse<UserMeal> apiResponse = new ApiResponse<>();
        if (userInfo == null || userInfo.getId() == null) {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).setMessage("修改用户信息失败").setSuccess(false);
        } else {
            switch (userMealService.modifyUserInfo(userInfo)) {
                case 1:
                    apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).setMessage("图片格式选择错误").setSuccess(false);
                    break;
                case 2:
                    apiResponse.setCode(SystemStatusCode.INTERNAL_SERVER_ERROR.getCode()).setMessage("修改用户信息失败").setSuccess(false);
                    break;
                default:
                    apiResponse.setCode(SystemStatusCode.SUCCESS.getCode())
                            .setMessage("修改用户信息成功")
                            .setSuccess(true)
                            .setData(userMealService.getById(userInfo.getId()));
            }
        }
        return apiResponse;
    }

    /**
     * 根据id修改用户设置（价格设置）
     *
     * @param userInfo 要修改的用户信息
     * @return 返回修改后的用户新信息
     */
    @PutMapping("/price")
    public ApiResponse<UserMeal> modifyPrice(@RequestBody UserDTO userInfo) {
        ApiResponse<UserMeal> apiResponse = new ApiResponse<>();
        if (userInfo == null || userInfo.getId() == null) {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).setMessage("修改用户设置失败").setSuccess(false);
        } else {
            UserMeal userMeal = new UserMeal()
                    .setId(userInfo.getId())
                    .setMaxFoodPrice(userInfo.getMaxFoodPrice())
                    .setMinFoodPrice(userInfo.getMinFoodPrice());
            if (userMealService.updateById(userMeal)) {
                apiResponse.setCode(SystemStatusCode.SUCCESS.getCode())
                        .setMessage("修改用户设置成功")
                        .setSuccess(true)
                        .setData(userMealService.getById(userInfo.getId()));
            } else {
                apiResponse.setCode(SystemStatusCode.INTERNAL_SERVER_ERROR.getCode())
                        .setMessage("修改用户设置失败")
                        .setSuccess(false);
            }
        }
        return apiResponse;
    }


    /**
     * 用户注销/删除用户接口，逻辑删除，而非物理删除。通过修改账号的状态位置实现
     *
     * @param userId 用户id
     * @return 返回删除结果
     */
    @DeleteMapping({"/{userId}"})
    public ApiResponse<UserMeal> deleteUserInfo(@PathVariable Integer userId) {
        ApiResponse<UserMeal> apiResponse = new ApiResponse<>();
        if (userId == null) {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).setMessage("删除用户信息失败").setSuccess(false);
        } else {
            if (userMealService.updateById(new UserMeal().setId(userId).setStatus(-1))) {
                apiResponse.setCode(SystemStatusCode.SUCCESS.getCode()).setMessage("删除用户信息成功").setSuccess(true);
            } else {
                apiResponse.setCode(SystemStatusCode.INTERNAL_SERVER_ERROR.getCode()).setMessage("删除用户信息失败").setSuccess(false);
            }
        }
        return apiResponse;
    }

    /**
     * 修改隐私模式
     *
     * @param userInfo 用户信息
     * @return 返回修改结果
     */
    @PutMapping("/privacy")
    public ApiResponse<String> modifyPrivacy(@RequestBody UserMeal userInfo) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (userInfo.getId() == null || userInfo.getPrivacy() == null) {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).setMessage("修改隐私模式失败").setSuccess(false);
        } else {
            if (userMealService.updateById(new UserMeal().setId(userInfo.getId()).setPrivacy(userInfo.getPrivacy()))) {
                apiResponse.setCode(SystemStatusCode.SUCCESS.getCode()).setMessage("修改隐私模式成功").setSuccess(true);
            } else {
                apiResponse.setCode(SystemStatusCode.INTERNAL_SERVER_ERROR.getCode()).setMessage("修改隐私模式失败").setSuccess(false);
            }
        }
        return apiResponse;
    }

}
